<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList1" @findClicked="findData" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 10%">
          <h1>大类资料</h1>
          <Table
            :tableHeader="tableHeader7"
            :tableData="LargeClassDataTableList1"
            :TableHeight="100"
            :outTableHeight="60"
            @currentRow="clicktogetdata"
            :isLoading="isLoading"
          />
        </div>

        <div style="width: 10%">
          <h1>中类资料</h1>
          <Table
            :tableHeader="tableHeader8"
            :tableData="LargeClassDataTableList2"
            :TableHeight="100"
            :outTableHeight="60"
            @currentRow="clicktogetdata1"
            :isLoading="isLoading"
          />
        </div>

        <div style="width: 10%">
          <h1>小类资料</h1>
          <Table
            :tableHeader="tableHeader9"
            :tableData="LargeClassDataTableList3"
            :TableHeight="100"
            :outTableHeight="60"
            @currentRow="clicktogetdata2"
            :isLoading="isLoading"
          />
        </div>

        <div style="width: 21%; margin-left: 10px">
          <h1>材料属性</h1>
          <Table
            @currentRow1="currentRow1"
            @checkboxChange="change"
            @currentRow="clicktogetdata9"
            :tableHeader="tableHeader1"
            :tableData="TableListDate1"
            :TableHeight="100"
            :outTableHeight="60"
            :isLoading="isLoading2"
            @inputChange="confirmChange5"
            :getTableList="MaterialPropertyMethod"
          />
        </div>

        <div style="width: 11%; margin-left: 10px">
          <h1>材料属性定义</h1>
          <Table
            @currentRow="clicktogetdata3"
            :tableHeader="tableHeader2"
            :tableData="MaterialPropertyDefinitionTableDateList"
            :TableHeight="100"
            :outTableHeight="60"
            :isLoading="isLoading1"
          />
        </div>

        <div style="width: 15%; margin-left: 10px">
          <h1>材料属性关联规格</h1>
          <Table
            @currentRow1="FitFinishDlck3"
            @currentRow="clicktogetdata8"
            :tableHeader="tableHeader3"
            :tableData="WideDefinitionMethodTableDateList"
            :TableHeight="100"
            :outTableHeight="60"
            :isLoading="isLoading3"
            @inputChange="confirmChange3"
            :getTableList="WideDefinitionMethod"
          />
        </div>

        <div style="width: 15%; margin-left: 10px">
          <h1>宽幅定义</h1>
          <Table
            @currentRow1="FitFinishDlck2"
            @currentRow="clicktogetdata7"
            :tableHeader="tableHeader10"
            :tableData="WideDefinitionListTableDate"
            :TableHeight="100"
            :outTableHeight="60"
            :isLoading="isLoading4"
            @inputChange="confirmChange2"
            :getTableList="WideDefinitionList"
          />
        </div>

        <div style="width: 18%; margin-left: 10px">
          <h1>单位定义</h1>
          <Table
            @currentRow1="FitFinishDlck1"
            :tableHeader="tableHeader5"
            :tableData="UnitDefinitionMethodTableDateList"
            :TableHeight="100"
            :outTableHeight="60"
            :isLoading="isLoading5"
            @currentRow="clicktogetdata6"
            @inputChange="confirmChange1"
            :getTableList="UnitDefinitionMethod"
          />
        </div>

        <div style="width: 22%; margin-left: 10px">
          <h1>贴合加工方式定义</h1>
          <Table
            @currentRow1="FitFinishDlck"
            :tableHeader="tableHeader6"
            :tableData="FitFinishTableDateList"
            :TableHeight="100"
            :outTableHeight="60"
            :isLoading="isLoading6"
            @currentRow="clicktogetdata5"
            @inputChange="confirmChange"
            :getTableList="FitFinish"
          />
        </div>
      </div>
      <PageModule :PageData="{ pageSizeShow: true }" />

      <el-dialog :close-on-click-modal="false" v-model="dialogVisible" title="添加数据" width="30%">
        <el-select
          style="width: 150px; margin-left: 10px; height: 33px"
          v-if="gatherString === '材料属性'"
          v-model="valueLeft"
          class="m-2"
          placeholder="材料属性"
          size="default"
        >
          <el-option
            @click="collectMaterialProperty(item)"
            v-for="item in MaterialPropertyDefinitionTableDateList"
            :key="item.seqNo"
            :label="item.mtlProperty"
            :value="item.mtlProperty"
          />
        </el-select>

        <el-checkbox v-if="gatherString === '材料属性'" style="margin-left: 20px" v-model="checked1" label="必选否" size="large" />

        <el-input
          v-for="(item, index) in gatherArrayList"
          :key="index"
          :placeholder="item"
          style="width: 150px; height: 30px; margin-left: 10px; margin-top: 10px"
          v-model="gatherArrayListParms[index]"
        />

        <el-checkbox style="margin-left: 10px" v-if="gatherString === '材料属性'" v-model="checked2" label="是否编码" size="large" />

        <template #footer>
          <span class="dialog-footer">
            <el-button @click=";(dialogVisible = false), (gatherArrayListParms = []), (checked1 = false), (checked2 = false)">取消</el-button>
            <el-button type="primary" @click="saveData"> 保存 </el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialAttributeDefinition'
})
</script>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import {
  materialAttributeDelete,
  MaterialPropertyAssociationDelete,
  WideDefinitionDelete,
  MaterialPropertiesAssociatedSpecificationsDelete,
  LargeClassData,
  materialAttribute,
  MaterialPropertyDefinition,
  MaterialPropertyAssociation,
  WideDefinition,
  UnitDefinition,
  FitProcessingMethod,
  FitProcessingAdd,
  UnitDefinitionAddition,
  WideDefinitionAdd,
  MaterialPropertiesAssociatedSpecificationsAdded,
  materialAttributeMethods,
  materialAdd,
  FitProcessingDelete,
  UnitDefinitionAdditionDelete
} from '@/api/ControlDepartment/MaterialManagement/MaterialAttributeDefinition'
import type {
  LargeClassDataInterface,
  materialAttributeParms,
  materialAttributeInterface,
  MaterialPropertyDefinitionInterface,
  WideDefinitionInterface,
  WideDefinitionInterfaceLct,
  UnitDefinitionInterface,
  FitProcessingMethodInterface
} from '@/api/ControlDepartment/MaterialManagement/MaterialAttributeDefinition'
import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'
const formList1 = ref([
  {
    type: 'button',
    checkboxLabel: '分类建档'
  },
  {
    type: 'button',
    checkboxLabel: '添加'
  },
  {
    type: 'button',
    checkboxLabel: '删除'
  }
])

// 定义必选否
const checked1 = ref<boolean>(false)
const checked2 = ref<boolean>(false)

const AddTypeNmae_TypeString = ref({
  typeName: '',
  typeName2: '',
  typeName3: '',
  typeNo: '',
  typeNo2: '',
  typeNo3: ''
})

// 材料属性删除
const materialAttributeP = ref<any>({
  mtlNoNeed: '',
  mtlProperty: '',
  propertyNeed: '',
  propertySeq: '',
  seqNo: ''
})

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      const result = await materialAttributeDelete(Object.assign({}, materialAttributeP.value, AddTypeNmae_TypeString.value))
      if (result.code === 200) {
        JMessage.success(result.message)
        MaterialPropertyMethod()
      } else JMessage.error(result.message)
    })
    .catch(() => {})
}

// 材料属性规格删除
const MaterialPropertyAssociationDeleteParms = ref({
  mtlProperty: '',
  mtlSpec: ''
})

const handleCloseMaterial = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      const result = await MaterialPropertyAssociationDelete(
        MaterialPropertyAssociationDeleteParms.value.mtlProperty,
        MaterialPropertyAssociationDeleteParms.value.mtlSpec
      )
      if (result.code === 200) {
        JMessage.success(result.message)
        WideDefinitionMethod()
      } else JMessage.error(result.message)
    })
    .catch(() => {})
}

// 宽幅定义删除
const WideDefinitionDeleteParms = ref<string>()

const handleCloseWide = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      const result = await WideDefinitionDelete(WideDefinitionDeleteParms.value!)
      if (result.code === 200) {
        JMessage.success(result.message)
        WideDefinitionList()
      } else JMessage.error(result.message)
    })
    .catch(() => {})
}

// 贴合加工方式
const FitFinishTableDateList = ref<any[]>()

// 单位定义列表
const UnitDefinitionMethodTableDateList = ref<any[]>()

const WideDefinitionMethodTableDateList = ref<any[]>()

// 宽幅定义
const WideDefinitionListTableDate = ref<any[]>()

const valueLeft = ref()

const valueLeft1 = ref()

const collectMaterialProperty = (Event: any) => {
  valueLeft1.value = Event
}

const seqStringValue = ref()

// 材料属性修改

const ubdateTop = ref({
  updateTop: {
    mtlProperty: '',
    propertyNeed: '',
    propertySeq: '',
    mtlNoNeed: '',
    seqNo: ''
  },
  updateEnd: {
    mtlProperty: '',
    propertyNeed: '',
    propertySeq: '',
    mtlNoNeed: '',
    seqNo: ''
  }
})

const FitFinishDlck5 = (row: any) => {
  TableListDate1.value?.forEach((item: any) => {
    item.change = 'Y'
  })
}

const currentRow1 = (row: any) => {
  TableListDate1.value?.forEach((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
  })
  ubdateTop.value.updateTop.mtlNoNeed = row.mtlNoNeed === true ? 'Y' : ''
  ubdateTop.value.updateTop.mtlProperty = row.mtlProperty
  ubdateTop.value.updateTop.propertyNeed = row.propertyNeed === true ? 'Y' : ''
  ubdateTop.value.updateTop.propertySeq = String(row.propertySeq)
  ubdateTop.value.updateTop.seqNo = String(row.seqNo)
}

const change = async (row: any, prop: any) => {
  console.log(prop)
  ubdateTop.value.updateEnd.mtlNoNeed = prop.mtlNoNeed === true ? 'Y' : ''
  ubdateTop.value.updateEnd.mtlProperty = prop.mtlProperty
  ubdateTop.value.updateEnd.propertyNeed = prop.propertyNeed === true ? 'Y' : ''
  ubdateTop.value.updateEnd.propertySeq = String(prop.propertySeq)
  ubdateTop.value.updateEnd.seqNo = String(prop.seqNo)
  ubdateTop.value.updateTop = Object.assign({}, ubdateTop.value.updateTop, AddTypeNmae_TypeString.value)
  ubdateTop.value.updateEnd = Object.assign({}, ubdateTop.value.updateEnd, AddTypeNmae_TypeString.value)

  const result = await LoginWork.put<any, any>('/MaterialManagement/getTypeDetailUpdate', ubdateTop.value)
  if (result.code === 200) {
    JMessage.success(result.message)
    WideDefinitionMethod()
    MaterialPropertyMethod()
  } else {
    JMessage.error(result.message)
  }
}

const confirmChange5 = async (row?: any) => {
  console.log(row)
  ubdateTop.value.updateEnd.mtlNoNeed = row.mtlNoNeed === true ? 'Y' : ''
  ubdateTop.value.updateEnd.mtlProperty = row.mtlProperty
  ubdateTop.value.updateEnd.propertyNeed = row.propertyNeed === true ? 'Y' : ''
  ubdateTop.value.updateEnd.propertySeq = String(row.propertySeq)
  ubdateTop.value.updateEnd.seqNo = String(row.seqNo)
  ubdateTop.value.updateTop = Object.assign({}, ubdateTop.value.updateTop, AddTypeNmae_TypeString.value)
  ubdateTop.value.updateEnd = Object.assign({}, ubdateTop.value.updateEnd, AddTypeNmae_TypeString.value)
  const result = await LoginWork.put<any, any>('/MaterialManagement/getTypeDetailUpdate', ubdateTop.value)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 材料属性关联规格修改
const confirmChange3 = async (row: any) => {
  const result = await LoginWork.put<any, any>('/MaterialManagement/getMaterialPropertiesSpecUpdate', row)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const FitFinishDlck3 = () => {
  WideDefinitionMethodTableDateList.value?.forEach((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
  })
}

// 宽幅定义修改
const confirmChange2 = async (row: any) => {
  const result = await LoginWork.put<any, any>('/MaterialManagement/getWideDefinitionUpdate', row)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const FitFinishDlck2 = () => {
  WideDefinitionListTableDate.value?.forEach((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
  })
}

// 单位定义修改
const confirmChange1 = async (row: any) => {
  const result = await LoginWork.post<any, any>('/MaterialManagement/getUnitAdd', row)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const FitFinishDlck1 = () => {
console.log('11')  
  UnitDefinitionMethodTableDateList.value?.forEach((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
  })
}

// 贴合加工方式定义修改
const confirmChange = async (row: any) => {
  const result = await LoginWork.put<any, any>('/MaterialManagement/getFitUpdate', row)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const FitFinishDlck = () => {
  FitFinishTableDateList.value?.forEach((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
  })
}

// 材料属性定义删除
const mtlProperty = ref<string>()

const handleClose6 = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      const result = await MaterialPropertiesAssociatedSpecificationsDelete(mtlProperty.value!)
      if (result.code === 200) {
        JMessage.success(result.message)
        WideDefinitionMethod()
      } else JMessage.error(result.message)
    })
    .catch(() => {})
}

// 单位定义删除
const mtlUnit = ref<string>()

const handleClose5 = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      const result = await UnitDefinitionAdditionDelete(mtlUnit.value!)
      if (result.code === 200) {
        JMessage.success(result.message)
        UnitDefinitionMethod()
      } else JMessage.error(result.message)
    })
    .catch(() => {})
}

// 贴合加工方式定义删除
const prdType = ref<string>()

const handleClose3 = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      const result = await FitProcessingDelete(prdType.value!)
      if (result.code === 200) {
        JMessage.success(result.message)
        FitFinish()
      } else JMessage.error(result.message)
    })
    .catch(() => {})
}

const dialogVisible = ref(false)

const lableString = ref(false)

const gatherString = ref<string>('')

const gatherArrayList = ref<any>([])

// 存储数据
const gatherArrayListParms = ref<any>([])

const mtlString = ref<string>()

const saveData = async () => {
  if (gatherString.value === '贴合加工方式定义') {
    const obj = gatherArrayListParms.value.reduce((acc: any, item: any, index: any) => {
      acc.seqNo = index === 0 ? item : acc.seqNo
      acc.prdType = index === 1 ? item : acc.prdType
      acc.sortNo = index === 2 ? item : acc.sortNo
      return acc
    }, {})
    let active = FitFinishTableDateList.value!.some((item: any) => {
      return item.seqNo === Number(obj.seqNo)
    })
    if (Object.keys(obj).length === 0) {
      JMessage.warning('序号或加工方式不可为空')
    } else if (active) {
      JMessage.warning('序号不可重复请重新输入')
    } else if (obj.prdType === undefined) {
      JMessage.warning('加工方式不可为空')
    } else if (isNaN(obj.seqNo) || isNaN(obj.sortNo)) {
      JMessage.warning('序号或排序必须是数字')
    } else {
      const result = await FitProcessingAdd(obj as any)
      if (result.code === 200) {
        JMessage.success(result.message)
        FitFinish()
        gatherArrayListParms.value = []
      } else {
        JMessage.error(result.message)
      }
      dialogVisible.value = false
    }
  }

  if (gatherString.value === '单位定义') {
    const obj = gatherArrayListParms.value.reduce((acc: any, item: any, index: any) => {
      acc.mtlUnit = index === 0 ? item : acc.mtlUnit
      acc.mtlSeq = index === 1 ? item : acc.mtlSeq
      acc.engMtlUnit = index === 2 ? item : acc.engMtlUnit
      acc.sortNo = index === 3 ? item : acc.sortNo
      return acc
    }, {})
    let active = UnitDefinitionMethodTableDateList.value!.some((item: any) => {
      return item.mtlSeq === obj.mtlSeq
    })

    if (Object.keys(obj).length === 0) {
      JMessage.warning('编码和中文单位不可为空')
    } else if (active) {
      JMessage.warning('输入的编码不可重复')
    } else if (obj.mtlSeq === undefined || obj.mtlSeq.length < 2 || obj.mtlSeq.length > 2) {
      JMessage.warning('输入的编码必须是2位数')
    } else if (obj.mtlUnit === undefined) {
      JMessage.warning('中文单位必须输入')
    } else if (isNaN(obj.mtlSeq) || isNaN(obj.sortNo)) {
      JMessage.warning('编码或排序必须是数字')
    } else {
      const result = await UnitDefinitionAddition(obj as any)
      if (result.code === 200) {
        JMessage.success(result.message)
        UnitDefinitionMethod()
        gatherArrayListParms.value = []
      } else {
        JMessage.error(result.message)
      }
      dialogVisible.value = false
    }
  }

  if (gatherString.value === '宽幅定义') {
    const obj = gatherArrayListParms.value.reduce((acc: any, item: any, index: any) => {
      acc.mtlSeq = index === 0 ? item : acc.mtlSeq
      acc.mtlSpec = index === 1 ? item : acc.mtlSpec
      acc.sortNo = index === 2 ? item : acc.sortNo
      return acc
    }, {})
    let active = WideDefinitionListTableDate.value!.some((item: any) => {
      return item.mtlSeq === obj.mtlSeq
    })

    if (Object.keys(obj).length === 0) {
      JMessage.warning('编码或宽幅不可为空')
    } else if (active) {
      JMessage.warning('编码不可重复请重新输入')
    } else if (obj.mtlSeq === undefined || obj.mtlSeq.length < 4 || obj.mtlSeq.length > 4) {
      JMessage.warning('编码必须是4位')
    } else if (obj.mtlSpec === undefined) {
      JMessage.warning('宽幅不可为空')
    } else if (isNaN(obj.mtlSeq) || isNaN(obj.sortNo)) {
      JMessage.warning('编码或排序必须是数字')
    } else {
      const result = await WideDefinitionAdd(obj as any)
      if (result.code === 200) {
        JMessage.success(result.message)
        WideDefinitionList()
        gatherArrayListParms.value = []
      } else {
        JMessage.error(result.message)
      }
      dialogVisible.value = false
    }
  }

  if (gatherString.value === '材料属性关联规格') {
    const obj = gatherArrayListParms.value.reduce((acc: any, item: any, index: any) => {
      acc.mtlSeq = index === 0 ? item : acc.mtlSeq
      acc.mtlSpec = index === 1 ? item : acc.mtlSpec
      acc.sortNo = index === 2 ? item : acc.sortNo
      acc.mtlProperty = mtlString.value
      return acc
    }, {})
    let active = WideDefinitionMethodTableDateList.value!.some((item: any) => {
      return item.mtlSeq === obj.mtlSeq
    })

    if (Object.keys(obj).length === 0) {
      JMessage.warning('编号或规格不可为空')
    } else if (active) {
      JMessage.warning('编号不可重复请重新输入')
    } else if (obj.mtlSeq === undefined || obj.mtlSeq.length < 4 || obj.mtlSeq.length > 4) {
      JMessage.warning('编号必须是4位')
    } else if (obj.mtlSpec === undefined) {
      JMessage.warning('规格不可为空')
    } else if (isNaN(obj.mtlSeq) || isNaN(obj.sortNo)) {
      JMessage.warning('编号或排序必须是数字')
    } else {
      const result = await MaterialPropertiesAssociatedSpecificationsAdded(obj as any)
      if (result.code === 200) {
        JMessage.success(result.message)
        WideDefinitionMethod()
        gatherArrayListParms.value = []
      } else {
        JMessage.error(result.message)
      }
      dialogVisible.value = false
    }
  }

  if (gatherString.value === '材料属性定义') {
    const obj = gatherArrayListParms.value.reduce((acc: any, item: any, index: any) => {
      acc.seqNo = index === 0 ? item : acc.seqNo
      acc.mtlProperty = index === 1 ? item : acc.mtlProperty
      return acc
    }, {})
    let active = MaterialPropertyDefinitionTableDateList.value!.some((item: any) => {
      return item.seqNo === Number(obj.seqNo)
    })
    if (Object.keys(obj).length === 0) {
      JMessage.warning('序号或材料属性不可为空')
    } else if (active) {
      JMessage.warning('序号不可重复请重新输入')
    } else if (obj.mtlProperty === undefined) {
      JMessage.warning('材料属性不可为空')
    } else if (obj.seqNo === undefined) {
      JMessage.warning('序号不可为空')
    } else if (isNaN(obj.seqNo)) {
      JMessage.warning('编号或排序必须是数字')
    } else {
      const result = await materialAttributeMethods(obj as any)
      if (result.code === 200) {
        JMessage.success(result.message)
        MaterialPropertyDefinitionMethods()
        gatherArrayListParms.value = []
      } else {
        JMessage.error(result.message)
      }
      dialogVisible.value = false
    }
  }

  if (gatherString.value === '材料属性') {
    const obj = {
      mtlProperty: valueLeft1.value ? valueLeft1.value.mtlProperty || '' : '',
      mtlNoNeed: checked2.value === true ? 'Y' : '',
      propertySeq: gatherArrayListParms.value[0],
      propertyNeed: checked1.value === true ? 'Y' : '',
      seqNo: valueLeft1.value ? valueLeft1.value.seqNo : '',
      typeName: AddTypeNmae_TypeString.value.typeName,
      typeName2: AddTypeNmae_TypeString.value.typeName2,
      typeName3: AddTypeNmae_TypeString.value.typeName3,
      typeNo: AddTypeNmae_TypeString.value.typeNo,
      typeNo2: AddTypeNmae_TypeString.value.typeNo2,
      typeNo3: AddTypeNmae_TypeString.value.typeNo3
    }

    if (valueLeft1.value === undefined) {
      JMessage.warning('材料属性不可为空')
    } else {
      const result = await materialAdd(obj as any)
      if (result.code === 200) {
        JMessage.success(result.message)
        MaterialPropertyMethod()
        gatherArrayListParms.value = []
        checked1.value = false
        checked2.value = false
      } else {
        JMessage.error(result.message)
      }
      dialogVisible.value = false
    }
  }

  lableString.value = true
}

const clicktogetdata5 = (row: any) => {
  gatherString.value = '贴合加工方式定义'
  prdType.value = row.prdType
  const newObject = Object.keys(row).map((item: any) => {
    if (item === 'seqNo') {
      item = '序号'
    }
    if (item === 'prdType') {
      item = '加工方式'
    }
    if (item === 'sortNo') {
      item = '排序'
    }
    return item
  })
  gatherArrayList.value = newObject
  console.log(gatherArrayList.value)
}

const clicktogetdata6 = (row: any) => {
  gatherString.value = '单位定义'
  mtlUnit.value = row.mtlUnit
  const newObject = Object.keys(row).map((item: any) => {
    if (item === 'mtlSeq') {
      item = '编码'
    }
    if (item === 'mtlUnit') {
      item = '中文单位'
    }
    if (item === 'engMtlUnit') {
      item = '英文单位'
    }
    if (item === 'sortNo') {
      item = '排序'
    }
    return item
  })
  gatherArrayList.value = newObject
}

const clicktogetdata7 = (row: any) => {
  gatherString.value = '宽幅定义'
  WideDefinitionDeleteParms.value = row.mtlSpec
  const newObject = Object.keys(row).map((item: any) => {
    if (item === 'mtlSeq') {
      item = '编码'
    }
    if (item === 'mtlSpec') {
      item = '宽幅'
    }
    if (item === 'sortNo') {
      item = '排序'
    }
    return item
  })
  gatherArrayList.value = newObject
}

const clicktogetdata8 = (row: any) => {
  gatherString.value = '材料属性关联规格'
  MaterialPropertyAssociationDeleteParms.value.mtlSpec = row.mtlSpec
  const newObject = Object.keys(row).map((item: any) => {
    if (item === 'mtlSeq') {
      item = '编号'
    }
    if (item === 'mtlSpec') {
      item = '规格'
    }
    if (item === 'sortNo') {
      item = '排序'
    }
    return item
  })
  gatherArrayList.value = newObject
}

const clicktogetdata9 = (row: any) => {
  console.log('111')
  gatherString.value = '材料属性'
  materialAttributeP.value.mtlNoNeed = row.mtlNoNeed === true ? 'Y' : ''
  materialAttributeP.value.propertyNeed = row.propertyNeed === true ? 'Y' : ''
  materialAttributeP.value.mtlProperty = row.mtlProperty
  materialAttributeP.value.propertySeq = String(row.propertySeq === null ? '' : row.propertySeq)
  materialAttributeP.value.seqNo = String(row.seqNo)

  const newObject = Object.keys(row).map((item: any) => {
    if (item === 'propertySeq') {
      item = '名称组成顺序'
    }
    return item
  })
  gatherArrayList.value = newObject.filter((item) => {
    return item !== 'change' && item !== 'mtlProperty' && item !== 'propertyNeed' && item !== 'mtlNoNeed' && item !== 'seqNo'
  })
}

const isLoading = ref(true)

const isLoading1 = ref(true)

const isLoading2 = ref(true)

const isLoading3 = ref(true)

const isLoading4 = ref(true)

const isLoading5 = ref(true)

const isLoading6 = ref(true)

const FitFinish = async () => {
  isLoading6.value = true
  const result = await FitProcessingMethod()
  if (result.length === 0) {
    FitFinishTableDateList.value = [{ seqNo: '', prdType: '', sortNo: '' }]
  } else {
    FitFinishTableDateList.value = result
  }
  isLoading6.value = false
}

const UnitDefinitionMethod = async () => {
  isLoading5.value = true
  const result = await UnitDefinition()
  if (result.length === 0) {
    UnitDefinitionMethodTableDateList.value = [{ mtlUnit: '', mtlSeq: '', engMtlUnit: '', sortNo: '' }]
  } else {
    UnitDefinitionMethodTableDateList.value = result
  }
  isLoading5.value = false
}

const WideDefinitionList = async () => {
  isLoading4.value = true
  const result = await WideDefinition()
  if (result.length === 0) {
    WideDefinitionListTableDate.value = [{ mtlSeq: '', mtlSpec: '', sortNo: '' }]
  } else {
    WideDefinitionListTableDate.value = result
  }
  isLoading4.value = false
}

// 材料属性关联规格
const WideDefinitionMethodParms = ref<string>('')

const clicktogetdata3 = (row: any) => {
  gatherString.value = '材料属性定义'
  mtlProperty.value = row.mtlProperty
  const newObject = Object.keys(row).map((item: any) => {
    if (item === 'seqNo') {
      item = '材料属性'
    }
    if (item === 'mtlProperty') {
      item = '序号'
    }
    return item
  })
  gatherArrayList.value = newObject
  MaterialPropertyAssociationDeleteParms.value.mtlProperty = row.mtlProperty
  WideDefinitionMethodParms.value = row.mtlProperty
  mtlString.value = row.mtlProperty
  WideDefinitionMethod()
}

const WideDefinitionMethod = async () => {
  isLoading3.value = true
  const result = await MaterialPropertyAssociation(WideDefinitionMethodParms.value)
  if (result.length === 0) {
    WideDefinitionMethodTableDateList.value = [{ mtlSeq: '', mtlSpec: '', sortNo: '' }]
  } else {
    WideDefinitionMethodTableDateList.value = result
  }

  isLoading3.value = false
}

const MaterialPropertyParameter = ref<materialAttributeParms>({
  typeNo: '',
  typeNo2: '',
  typeNo3: ''
})

// 材料属性定义
const MaterialPropertyDefinitionTableDateList = ref<any[]>()

const MaterialPropertyDefinitionMethods = async () => {
  isLoading1.value = true
  const result = await MaterialPropertyDefinition()
  if (result.length === 0) {
    MaterialPropertyDefinitionTableDateList.value = [{ mtlProperty: '', seqNo: '' }]
  } else {
    MaterialPropertyDefinitionTableDateList.value = result
  }
  isLoading1.value = false
  if (result.length !== 0) {
    WideDefinitionMethodParms.value = result[0].mtlProperty
    mtlString.value = result[0].mtlProperty
    seqStringValue.value = result[0].seqNo
    WideDefinitionMethod()
  }
}

const TableListDate1 = ref<any[]>()

const MaterialPropertyMethod = async () => {
  isLoading2.value = true
  const result = await materialAttribute(MaterialPropertyParameter.value)
  result.forEach((item: any) => {
    if (item.propertyNeed === 'Y') {
      item.propertyNeed = true
    }
    if (item.mtlNoNeed === 'Y') {
      item.mtlNoNeed = true
    }
  })
  if (result.length === 0) {
    TableListDate1.value = [{ mtlProperty: '', propertyNeed: '', propertySeq: 1, mtlNoNeed: '', seqNo: '' }]
  } else {
    TableListDate1.value = result
  }

  isLoading2.value = false
}

const clicktogetdata = (row: any) => {
  LargeClassDataTableList2.value = row.children

  LargeClassDataTableList3.value = row.children[0].children || ''

  MaterialPropertyParameter.value.typeNo = row.typeNo

  if (row.children && row.children[0]) {
    MaterialPropertyParameter.value.typeNo2 = row.children[0].typeNo || ''

    

    AddTypeNmae_TypeString.value.typeName = row.typeName

    AddTypeNmae_TypeString.value.typeName2 = row.children[0].typeName

    AddTypeNmae_TypeString.value.typeNo = row.children[0].typeNo

    AddTypeNmae_TypeString.value.typeNo2 = row.children[0].typeNo

    if (row.children[0].children && row.children[0].children[0]) {
      MaterialPropertyParameter.value.typeNo3 = row.children[0].children[0].typeNo || ''

      AddTypeNmae_TypeString.value.typeName3 = row.children[0].children[0].typeName

      AddTypeNmae_TypeString.value.typeNo = row.children[0].children[0].typeNo
    } else {
      MaterialPropertyParameter.value.typeNo = ''
    }
  } else {
    MaterialPropertyParameter.value.typeNo2 = ''
    MaterialPropertyParameter.value.typeNo3 = ''
  }
  
  MaterialPropertyMethod()
}

const clicktogetdata1 = (row: any) => {
  LargeClassDataTableList3.value = row.children

  MaterialPropertyParameter.value.typeNo2 = row.typeNo

  if (row.children && row.children[0]) {
    AddTypeNmae_TypeString.value.typeName3 = row.children[0].typeName

    AddTypeNmae_TypeString.value.typeNo3 = row.children[0].typeNo
  }

  AddTypeNmae_TypeString.value.typeName2 = row.typeName

  AddTypeNmae_TypeString.value.typeNo2 = row.typeNo

  MaterialPropertyParameter.value.typeNo3 = (row.children?.[0]?.typeNo ?? '') || ''

  MaterialPropertyMethod()
}

const clicktogetdata2 = (row: any) => {
  MaterialPropertyParameter.value.typeNo3 = row.typeNo

  AddTypeNmae_TypeString.value.typeName3 = row.typeName

  AddTypeNmae_TypeString.value.typeNo3 = row.typeNo

  MaterialPropertyMethod()
}

// 获取 大类中类小类
const LargeClassDataTableList1 = ref<LargeClassDataInterface[]>()

const LargeClassDataTableList2 = ref<any>()

const LargeClassDataTableList3 = ref<any>()

const LargeClassDataMethods = async () => {
  isLoading.value = true

  const result = await LargeClassData()

  if (result.length !== 0) {
    LargeClassDataTableList1.value = result as any

    MaterialPropertyParameter.value.typeNo = result[0].typeNo

    LargeClassDataTableList2.value = result[0].children

    MaterialPropertyParameter.value.typeNo2 = result[0].children[0].typeNo

    LargeClassDataTableList3.value = result[0].children[0].children

    MaterialPropertyParameter.value.typeNo3 = result[0].children[0].children[0].typeNo

    AddTypeNmae_TypeString.value.typeName = result[0].typeName

    AddTypeNmae_TypeString.value.typeName2 = result[0].children[0].typeName

    AddTypeNmae_TypeString.value.typeName3 = result[0].children[0].children[0].typeName

    AddTypeNmae_TypeString.value.typeNo = result[0].typeNo

    AddTypeNmae_TypeString.value.typeNo2 = result[0].children[0].typeNo

    AddTypeNmae_TypeString.value.typeNo3 = result[0].children[0].children[0].typeNo
  }
  MaterialPropertyMethod()
  isLoading.value = false
}

// 表格数据
const tableHeader1 = ref([
  {
    label: '材料属性',
    prop: 'mtlProperty'
  },
  {
    label: '必选否',
    type: 'checkbox',
    prop: 'propertyNeed'
  },
  {
    label: '名称组成顺序',
    prop: 'propertySeq'
  },
  {
    label: '是否编码',
    type: 'checkbox',
    prop: 'mtlNoNeed'
  }
])

const tableHeader2 = ref([
  {
    label: '序号',
    prop: 'seqNo',
    width: '50'
  },
  {
    label: '材料属性',
    prop: 'mtlProperty',
    width: '55'
  }
])

const tableHeader3 = ref([
  {
    label: '编号',
    prop: 'mtlSeq',
    width: '45',
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '排序',
    prop: 'sortNo'
  }
])

const tableHeader5 = ref([
  {
    label: '编码',
    prop: 'mtlSeq',
    width: '55',
    change: true
  },
  {
    label: '中文单位',
    prop: 'mtlUnit',
    width: '50',
    change: true
  },
  {
    label: '英文单位',
    prop: 'engMtlUnit',
    width: '50'
  },
  {
    label: '排序',
    prop: 'sortNo',
    width: '55'
  }
])

const tableHeader6 = ref([
  {
    label: '序号',
    prop: 'seqNo',
    width: '40',
    change: true
  },
  {
    label: '加工方式',
    prop: 'prdType',
    change: true,
    width: '130'
  },
  {
    label: '排序',
    prop: 'sortNo',
    width: '40'
  }
])

const tableHeader7 = ref([
  {
    label: '大类',
    prop: 'typeName',
    width: '100'
  }
])

const tableHeader8 = ref([
  {
    label: '中类',
    prop: 'typeName',
    width: '100'
  }
])

const tableHeader9 = ref([
  {
    label: '小类',
    prop: 'typeName',
    width: '100'
  }
])

const tableHeader10 = ref([
  {
    label: '编码',
    prop: 'mtlSeq',
    width: '45',
    change: true
  },
  {
    label: '宽幅',
    prop: 'mtlSpec',
    width: '50',
    change: true
  },
  {
    label: '排序',
    prop: 'sortNo'
  }
])

// 统一调用接口
const interAceFunct = () => {
  // 大类 中类 小类
  LargeClassDataMethods()

  // 材料属性定义
  MaterialPropertyDefinitionMethods()

  // 宽幅定义
  WideDefinitionList()

  //单位定义
  UnitDefinitionMethod()

  // 贴合加工方式定义
  FitFinish()
}

onMounted(() => {
  interAceFunct()
})

const findData = (find: string) => {
  if (find === '添加') {
    if (gatherString.value === '贴合加工方式定义') {
      dialogVisible.value = true
    }
    if (gatherString.value === '单位定义') {
      dialogVisible.value = true
    }
    if (gatherString.value === '宽幅定义') {
      dialogVisible.value = true
    }
    if (gatherString.value === '材料属性关联规格') {
      dialogVisible.value = true
    }
    if (gatherString.value === '材料属性定义') {
      dialogVisible.value = true
    }
    if (gatherString.value === '材料属性') {
      lableString.value = false
      dialogVisible.value = true
    }
  }

  if (find === '删除') {
    if (gatherString.value === '贴合加工方式定义') {
      handleClose3()
    }
    if (gatherString.value === '单位定义') {
      handleClose5()
    }
    if (gatherString.value === '宽幅定义') {
      handleCloseWide()
    }
    if (gatherString.value === '材料属性关联规格') {
      handleCloseMaterial()
    }
    if (gatherString.value === '材料属性定义') {
      handleClose6()
    }
    if (gatherString.value === '材料属性') {
      handleCloseMateriaLKlc()
    }
  }
}
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
